package utils

import (
	"fmt"
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

// Db 数据库连接
//var Db *gorm.DB

// InitMysql 初始化数据库 "root:abc123456def@(127.0.0.1:3306)/utils?charset=utf8mb4&parseTime=True&loc=Local"
func InitMysql(MysqlUserConf string) *gorm.DB {
	db, err := gorm.Open(mysql.Open(MysqlUserConf), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{SingularTable: true},
	})
	if err != nil {
		panic("连接数据库失败")
	}
	// defer Db.Close()
	fmt.Println("--- mysql init success :", MysqlUserConf, "---")

	//AdminDb = db
	return db
}

type TimeMixin struct {
	ID        int64          `gorm:"AUTO_INCREMENT type:bigint;primary_key" json:"id"`
	CreatedAt time.Time      `gorm:"column:createdAt;type:TIMESTAMP(6);default:CURRENT_TIMESTAMP(6);index" json:"createdAt"`
	UpdatedAt time.Time      `gorm:"column:updatedAt;type:TIMESTAMP(6);default:CURRENT_TIMESTAMP(6);index" json:"updatedAt"`
	DeletedAt gorm.DeletedAt `gorm:"index;column:deleteAt"`
}
